package com.ruoyi.activiti.controller;

import com.ruoyi.activiti.domain.TaskCondition;
import com.ruoyi.activiti.domain.TaskVo;
import com.ruoyi.activiti.domain.constant.TaskAction;
import com.ruoyi.activiti.service.MyTaskService;
import com.ruoyi.activiti.service.ProcessInstanceService;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.AjaxPageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags="任务")
@Validated//有此注解，才会校验游离的参数
public class TaskController extends BaseController {

    @Autowired
    private MyTaskService myTaskService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ProcessInstanceService processService;

    @ApiOperation(value = "查询任务列表", notes = "只查询未完成的任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码，从1开始，默认1", paramType = "body", defaultValue = "1")
            ,@ApiImplicitParam(name = "pageSize", value = "每页条数，默认20", paramType = "body", defaultValue = "20")
    })
    @PostMapping("task/list")
    public AjaxResult list(@RequestBody TaskCondition taskCondition){
        List<TaskVo> list = myTaskService.taskList(taskCondition);
        return AjaxPageResult.success(list);
    }

    @ApiOperation(value = "查询任务", notes = "")
    @GetMapping("task/{taskId}")
    public AjaxResult list(@PathVariable String taskId){
       return AjaxResult.success(myTaskService.getTaskVoById(taskId));
    }

    @ApiOperation(value = "查询我的待办", notes = "")
    @ApiImplicitParams({
    })
    @PostMapping("task/taskTodoList")//get在某些环境中不能传body参数，所以改为post，并且body参数的注释应在对象内
    public AjaxPageResult taskList(@RequestBody TaskCondition taskCondition) {
        taskCondition.setUsername(SecurityUtils.getUsername());
        List<TaskVo> list = myTaskService.taskList(taskCondition);
        return AjaxPageResult.success(list);
    }

    @ApiOperation(value = "查询我的已办", notes = "实际查询的是流程实例")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码，从1开始，默认1", paramType = "form", defaultValue = "1")
            ,@ApiImplicitParam(name = "pageSize", value = "每页条数，默认20", paramType = "form", defaultValue = "20")
    })
    @GetMapping("task/taskDoneList")
    public AjaxPageResult taskDoneList() {
        return SpringUtils.getBean(ProcessInstanceController.class).list(null, null, SecurityUtils.getUsername(), null, null);
    }

    @ApiOperation(value = "签收任务", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", paramType = "path")
    })
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("task/claim/{taskId}")
    public AjaxResult claim(@PathVariable String taskId) {
        taskService.claim(taskId, SecurityUtils.getUsername());
        return success();
    }

    @ApiOperation(value = "完成任务", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", paramType = "path")
    })
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("task/complete/{taskId}")
    public AjaxResult complete(@PathVariable String taskId, @RequestBody TaskVo task) {
        task.setTaskId(taskId);
        task.setAction(TaskAction.COMPLETE);
        myTaskService.doTask(task);
        return success();
    }

    @ApiOperation(value = "跳转任务", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", paramType = "path")
    })
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("task/jump/{taskId}")
    public AjaxResult jump(@PathVariable String taskId, @RequestBody TaskVo task) throws Exception {
        //processService.delegate(taskId, SecurityUtils.getUsername(), delegateToUser);
        task.setTaskId(taskId);
        task.setAction(TaskAction.JUMP);
        if(StringUtils.isEmpty(task.getTargetActivityId())){
            return AjaxResult.error("目标节点id不能为空");
        }
        myTaskService.doTask(task);
        return success();
    }

    @ApiOperation(value = "转办任务", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", paramType = "path")
            ,@ApiImplicitParam(name = "delegateToUser", value = "转办的目标用户", paramType = "form", defaultValue = "body")
            ,@ApiImplicitParam(name = "actionName", value = "处理方式中文，如：同意、不同意等", paramType = "body")
            ,@ApiImplicitParam(name = "comment", value = "处理意见", paramType = "body")
    })
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("task/delegate/{taskId}")
    public AjaxResult delegate(@PathVariable String taskId, @RequestBody TaskVo task) throws Exception {
        //processService.delegate(taskId, SecurityUtils.getUsername(), delegateToUser);
        task.setTaskId(taskId);
        task.setAction(TaskAction.DELEGATE);
        myTaskService.doTask(task);
        return success();
    }

    @ApiOperation(value = "回退任务", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", paramType = "path")
    })
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("task/back/{taskId}")
    public AjaxResult back(@PathVariable String taskId, @RequestBody TaskVo task) throws Exception {
        task.setTaskId(taskId);
        task.setAction(TaskAction.BACK);
        myTaskService.doTask(task);
        return success();
    }

    @ApiOperation(value = "驳回任务", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", paramType = "path")
    })
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("task/reject/{taskId}")
    public AjaxResult reject(@PathVariable String taskId, @RequestBody TaskVo task) {
        task.setTaskId(taskId);
        task.setAction(TaskAction.REJECT);
        myTaskService.doTask(task);
        return success();
    }

    @ApiOperation(value = "统一处理任务", notes = "必须要传action")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务id", paramType = "path")
    })
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("task/doTask/{taskId}")
    public AjaxResult doTask(@PathVariable String taskId, @RequestBody TaskVo task) {
        task.setTaskId(taskId);
        myTaskService.doTask(task);
        return success();
    }
}
